<template>
  <div class="page-container">
    <!--表格内容栏-->
    <cm-table
      ref="dataTable"
      pkName="id"
      :filters="filters"
      :filterFields="filterFields"
      :columns="columns"
      @handleTableOperator="handleTableOperator"
      @handleCellOperator="handleCellOperator"
    ></cm-table>

    <!--新增/编辑/查看界面-->
    <el-dialog
      width="100%"
      v-model="dialogVisible"
      :title="dialogTitle"
      :close-on-click-modal="false"
      destroy-on-close
      :fullscreen="true"
    >
      <el-form
        :model="formData"
        ref="dataForm"
        label-width="120px"
        label-position="right"
        :disabled="!dialogEdit"
        :rules="rules" 
        :hide-required-asterisk="!dialogEdit"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="消息类型" prop="type">
              <cm-radio v-model="formData.type" dic-code="dicMessageType"></cm-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接收人员" prop="receiveType">
              <cm-radio v-model="formData.receiveType" dic-code="dicMessageReceiveType"></cm-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="formData.receiveType === '02'">
          <el-col :span="24">
            <el-form-item label="接收人员" prop="employeeIds">
              <cm-employee v-model="formData.employeeIds" :multiple="true"></cm-employee>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input v-model="formData.title"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <cm-rich-editor v-model="formData.content"></cm-rich-editor>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="!dialogEdit">
          <el-col :span="12">
            <el-form-item label="更新时间" prop="updateTime">
              <el-input v-model="formData.updateTime"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-input v-model="formData.createTime"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button 
          v-if="dialogEdit"
          size="large"
          type="info"
          @click="dialogVisible=false"
        >取消</el-button>
        <el-button 
          v-if="dialogEdit"
          size="large"
          type="primary"
          @click="submitForm"
          :loading="editLoading"
        >提交</el-button>
        <el-button 
          v-if="!dialogEdit"
          size="large"
          type="info"
          @click="dialogVisible=false"
        >关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {ref, nextTick} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import api from '@/http/api'

const filters = ref({
  name: ''
})
      // 分页查询条件
const filterFields = ref([
  {prop: "title", label: "标题"}
])

// 分页列表字段
const columns = ref([
  {prop: "title", label: "标题", minWidth: 120, click: true},
  {prop: "type", label: "消息类型", width: 100, type: 'radio', dicCode: 'dicMessageType'},
  {prop: "receiveType", label: "接收人员类型", width: 120, type: 'select', dicCode: 'dicMessageReceiveType'},
  {prop: "updateTime", label: "更新时间", width: 160},
  {prop: "createTime", label: "创建时间", width: 160}
])

// 表单数据验证规则
const rules = ref({
  type: [
    {required: true, message: '请选择消息类型', trigger: 'change'},
    {max: 2, message: '长度必须小于等于 2 个字符！', trigger: 'change'}        
  ],
  receiveType: [
    {required: true, message: '请选择接收人员类型', trigger: 'change'},
    {max: 2, message: '长度必须小于等于 2 个字符！', trigger: 'change'}        
  ],
  employeeIds: [
    {required: true, message: '请选择接收人员', trigger: 'change'},
  ],
  title: [
    {required: true, message: '请输入标题', trigger: 'change'},
    {max: 500, message: '长度必须小于等于 500 个字符！', trigger: 'change'}        
  ],
  content: [
    {required: true, message: '请输入内容', trigger: 'change'},
    {max: 4294967295, message: '长度必须小于等于 4294967295 个字符！', trigger: 'change'}        
  ],
})

const dataTable = ref() // 弹窗表单数据
const dataForm = ref() // 弹窗表单数据
const formData = ref({}) // 弹窗表单数据
const dialogTitle = ref('') // 新增/编辑/查看弹窗标题
const dialogVisible = ref(false) // 弹窗是否显示
const editLoading = ref(false) // 弹窗表单提交状态
const dialogEdit = ref(false) // 弹窗是否编辑

const handleCellOperator = (property, value, row) => {
  formData.value = Object.assign({}, row);
  dialogVisible.value = true;
  dialogEdit.value = false;
  dialogTitle.value = "查看";

  nextTick(() => {
    dataForm.value && dataForm.value.clearValidate();
  });

  if ((row.ifRead || '') === '' || (row.ifRead || '') === '0') {
    api.post('/message/updateRead', {id: row.id}).then(() => row.ifRead = 1);
  }
}

// 显示操作界面
const handleTableOperator = (button, row) => {
  dialogVisible.value = true;
  dialogEdit.value = true;
  dialogTitle.value = button.name;

  if (button.buttonType === 'edit') {
    formData.value = Object.assign({}, row, {button: button});
  } else {
    formData.value = {button: button};
  }

  nextTick(() => {
    dataForm.value && dataForm.value.clearValidate();
  });
}

// 表单提交
const submitForm = () => {
  dataForm.value && dataForm.value.validate((valid) => {
    if (valid) {
      ElMessageBox.confirm('请确认是否提交？', '提示', {type: 'info'}).then(() => {
        editLoading.value = true;
        api.post(formData.value.button.dataDestUrl, formData.value).then(res => {
          ElMessage.success(res.message);
          dialogVisible.value = false;
          dataTable.value.findPage();
          editLoading.value = false;
        }).catch(() => {
          editLoading.value = false;
        });
      }).catch(()=>{});
    } else {
      ElMessage.error('数据录入不正确，请重新检查！');
    }
  })
}
</script>